package com.rules.admin.utils;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

public class CookieUtils {
    public static final String COOKIE_PAGE_SIZE = "_cookie_page_size";
    public static final int DEFAULT_SIZE = 20;
    public static final int MAX_SIZE = 200;
    public static final String ROOT_PATH = "/";

    public CookieUtils() {
    }

    public static int getPageSize() {
        Cookie cookie = getCookie("_cookie_page_size");
        int count = 0;
        if (cookie != null) {
            try {
                count = Integer.parseInt(cookie.getValue());
            } catch (Exception var3) {
            }
        }

        if (count <= 0) {
            count = 20;
        } else if (count > 200) {
            count = 200;
        }

        return count;
    }

    public static Cookie getCookie(String name) {
        HttpServletRequest request = RequestAndResponseContextHolder.request();
        if (request != null) {
            Cookie[] cookies = request.getCookies();
            if (ArrayUtils.isNotEmpty(cookies)) {
                for(int i = 0; i < cookies.length; ++i) {
                    Cookie cookie = cookies[i];
                    if (cookie.getName().equals(name)) {
                        return cookie;
                    }
                }
            }
        }

        return null;
    }

    public static final Cookie addCookie(String name, String value) {
        return addCookie(name, value, (String)null, (String)null);
    }

    public static final Cookie addCookie(String name, String value, String domain, String path) {
        return addCookie(name, value, domain, path, (Integer)null);
    }

    public static final Cookie addCookie(String name, String value, String domain, String path, Integer expiry) {
        path = defaultPathIfBlank(path);
        expiry = (Integer)ObjectUtils.defaultIfNull(expiry, Integer.MAX_VALUE);
        if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(value)) {
            Cookie cookie = new Cookie(name, value);
            cookie.setDomain(domain);
            cookie.setPath(path);
            cookie.setMaxAge(expiry);
            addCookie(cookie);
        }

        return null;
    }

    public static final Cookie addCookie(Cookie cookie) {
        HttpServletResponse response = RequestAndResponseContextHolder.response();
        if (response != null) {
            response.addCookie(cookie);
            return cookie;
        } else {
            return null;
        }
    }

    private static String defaultPathIfBlank(String path) {
        if (StringUtils.isBlank(path)) {
            HttpServletRequest request = RequestAndResponseContextHolder.request();
            if (request != null) {
                return (String)StringUtils.defaultIfBlank(request.getContextPath(), "/");
            }
        }

        return path;
    }

    public static void cancelCookie(String name, String domain) {
        Cookie cookie = new Cookie(name, (String)null);
        cookie.setMaxAge(0);
        cookie.setPath("/");
        if (!StringUtils.isBlank(domain)) {
            cookie.setDomain(domain);
        }

        HttpServletResponse response = RequestAndResponseContextHolder.response();
        if (response != null) {
            response.addCookie(cookie);
        }

    }
}
